package com.robotserver.orders.jobs;

import com.robotserver.orders.service.ListenTokenRunService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Auther: jianghan
 * @Date: 2020/4/26 14:40
 * @Description:监控联盟心跳，发现一段时间没更新订单，就启动该程序来更新订单
 */
@Component
public class ListenTokenRunJob {
    private static Logger logger = LoggerFactory.getLogger(ListenTokenRunJob.class);

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private ListenTokenRunService listenTokenRunService;

//    @Scheduled(cron="0 0/2 * * * ?")
    public void execute(){
        logger.debug("ListenTokenRunJob execute.");

        RLock lock = redissonClient.getLock("service_listen_token_run_job_lock");
        boolean getlock = false;
        try {
            if (getlock=lock.tryLock(0,1, TimeUnit.MINUTES)) {
                logger.debug("execute right.");
                listenTokenRunService.listen();
            }else {
                logger.debug("not execute right.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (getlock){
                lock.unlock();
                logger.debug("release lock.");
            }
        }
    }
}
